<?php
/**
 * Created by PhpStorm.
 * User: kankan
 * Date: 2020-04-28
 * Time: 09:53
 */

namespace App\Services;


use GuzzleHttp\Client;

class CurlServices
{
    /**
     * 发送GET请求
     *
     * @param string $url    请求地址
     * @param array  $params 请求参数
     *
     * @return string
     */
    static public function get(string $url, array $params = [], $timeout = 3)
    {
        $options       = array_merge(['query' => $params], ['timeout' => $timeout]);
        $response      = (new Client())->request('GET', $url, $options);
        $response_data = $response->getBody()->getContents();
        return $response_data;
    }

    /**
     * 发送POST请求
     *
     * @param string $url    请求地址
     * @param array  $params 请求参数
     *
     * @return string
     */
    static public function post(string $url, array $params)
    {
        $response      = (new Client())->request('POST',
            $url,
            [
                'body'    => json_encode($params, JSON_UNESCAPED_UNICODE),
                'headers' => ['content-type' => 'application/json']
            ]);
        $response_data = $response->getBody()->getContents();

        return $response_data;
    }

}